package com.zmxstudy.rbac.mapper;

import com.zmxstudy.rbac.entity.Auth;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zmxstudy.rbac.entity.RoleAuth;
import jakarta.validation.constraints.Future;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
* @author star
* @description 针对表【auth(系统菜单)】的数据库操作Mapper
* @createDate 2024-04-03 15:37:14
* @Entity com.zmxstudy.rbac.entity.Auth
*/
@Component
public interface AuthMapper extends BaseMapper<Auth> {
   @Select("<script>"+
           "select uri from  auth where id in" +
           "<foreach collection='authIds' Item='item' open ='(' close = ')' separator=','>"+
           "#{item}" +
           "</foreach>" +
           "</script>")
    List<String> selectUriByIds(@Param("authIds") List<Long> authIds);

    /**
     * 获取用户菜单列表
     * @param authIds
     * @return
     */
    @Select("<script>"+
            "select id,pid,name,path,redirect,component,meta_title,meta_icon,is_hidden from  auth where type in ('1','2') and id in" +
            "<foreach collection='authIds' item='item' open ='(' close = ')' separator=','>"+
            "#{item}" +
            "</foreach>" +
            "</script>")
//   @Select("select id,pid,name,path,redirect,component,meta_title,meta_icon,is_hidden from auth where type in ('1','2')")
    List<Auth> selectMenuByUsername(@Param("authIds") List<Long> authIds);


   @Select("SELECT role_auth.role_id,role_auth.auth_id,rolename,name from role JOIN role_auth on role.id=role_auth.role_id JOIN auth on role_auth.auth_id=auth.id where auth.pid=1")
    List<RoleAuth> AllRoleAuths();

    @Delete("delete from role_auth where role_id = #{roleId} and auth_id = #{authId}" )
    boolean delRoleAuth(@Param("roleId") int roleId,@Param("authId") int authId);


    @Insert("insert into role_auth values (#{roleId},#{authId})")
    boolean plusRoleAuth(@Param("roleId") int roleId,@Param("authId") int authId);

    @Select("<script>"+
            "select auth_id from  role_auth where role_id in" +
            "<foreach collection='roleIds' item='item' open ='(' close = ')' separator=','>"+
            "#{item}" +
            "</foreach>" +
            "</script>")
    List<Long> findAuthIds (@Param("roleIds") List<Long> roleIds);

}



